import { fabric } from 'fabric'
/**
 * 辅助线
 */
export class SubLine {
  private horizontalLine: fabric.Line | null
  private verticalLine: fabric.Line | null
  private canvas: fabric.Canvas

  constructor(canvas: fabric.Canvas) {
    this.horizontalLine = null
    this.verticalLine = null
    this.canvas = canvas
  }
  //增加辅助线
  init() {
    this.horizontalLine = new fabric.Line([-99999, 0, 99999, 0], {
      stroke: 'rgba(255, 245, 109, 0.70',
      strokeWidth: 1,
      selectable: false,
      evented: false,
      visible: false
    })
    this.verticalLine = new fabric.Line([0, -99999, 0, 99999], {
      stroke: 'rgba(255, 245, 109, 0.70',
      strokeWidth: 1,
      selectable: false,
      evented: false,
      visible: false
    })
    this.canvas.add(this.horizontalLine, this.verticalLine)
  }
  setVisible(visible: boolean) {
    if (!this.horizontalLine || !this.verticalLine) {
      console.error('请先初始化辅助线')
      return
    }
    this.horizontalLine?.set({ visible })
    this.verticalLine?.set({ visible })
    this.canvas.bringToFront(this.horizontalLine)
    this.canvas.bringToFront(this.verticalLine)
    this.canvas.renderAll()
  }
  updatePosition(x: number, y: number) {
    if (!this.horizontalLine || !this.verticalLine) {
      console.error('请先初始化辅助线')
      return
    }
    // 更新光标辅助线的位置
    this.horizontalLine.set({ y1: y, y2: y })
    this.verticalLine.set({ x1: x, x2: x })
  }
}
